﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HttpDownloader
{
    public partial class DownloadProcess : Form
    {
        private Int64 iCurrentDownloaded=0;
        private Int64 iTotalSize=0;
        private double iTransferRate=0;
        public double timeLeft=0;
        readonly object syncLock = new object();
        string str = "";
        public string strTimeLeft="";
        public DownloadProcess()
        {
            InitializeComponent();

        }

        public void ShowDownloadProcess()
        {
            this.Visible = true;
        }

        public void setFixInfo(string url, Int64 fileSize)
        {
            iTotalSize = fileSize;
            double size = fileSize;

            m_pLblUrlDownload.Text = url;

            lock (syncLock)
            {
                if (size >= Math.Pow(2, 30))
                {
                    size = size / (1024 ^ 3);
                    m_pLblFileSize.Text = String.Format("{0:f3} GB", size);
                    return;
                }
                if (size >= Math.Pow(2, 20))
                {
                    size = size / Math.Pow(2, 20);
                    m_pLblFileSize.Text = String.Format("{0:f3} MB", size);
                    return;
                }
                if (size >= Math.Pow(2, 10))
                {
                    size = size / Math.Pow(2, 10);
                    m_pLblFileSize.Text = String.Format("{0:f3} KB", size);
                    return;
                }
                if (size < Math.Pow(2, 10))
                {
                    m_pLblFileSize.Text = String.Format("{0:f3} B", iTotalSize);
                    return;
                }
            }
        }


        public void setDownloaded(Int64 currentDownload)
        {
            lock (syncLock)
            {
                iCurrentDownloaded = currentDownload;
                double percent = ((double)currentDownload / (double)iTotalSize) * 100;
                double size = currentDownload;

                m_pProcess.Value = (int)percent;

                if (size >= Math.Pow(2, 30))
                {
                    size = size / Math.Pow(2, 30);
                    str = String.Format("{0:f3} GB ", size);
                    str += String.Format(" ({0:f3} %)", percent);
                    m_pLblDownloaded.Text = str;
                    return;
                }
                if (size >= Math.Pow(2, 20))
                {
                    size = size / Math.Pow(2, 20);
                    str = String.Format("{0:f3} MB ", size);
                    str += String.Format(" ({0:f3} %)", percent);
                    m_pLblDownloaded.Text = str;
                    return;
                }
                if (size >= Math.Pow(2, 10))
                {
                    size = size / Math.Pow(2, 10);
                    str = String.Format("{0:f3} KB ", size);
                    str += String.Format(" ({0:f3} %)", percent);
                    m_pLblDownloaded.Text = str;
                    return;
                }
                if (currentDownload < 1024)
                {
                    str = String.Format("{0:f3} B ", currentDownload);
                    str += String.Format(" ({0:f3} %)", percent);
                    m_pLblDownloaded.Text = str;
                    return;
                }
            }
            
        }

        public void setTransferRate(double bytesPerSecond)
        {
            lock (syncLock)
            {
                iTransferRate = bytesPerSecond;
                if (bytesPerSecond > Math.Pow(2, 30))
                {
                    bytesPerSecond = bytesPerSecond / Math.Pow(2, 30);
                    m_pLblTransferRate.Text = String.Format("{0:f3} GB/s", bytesPerSecond);
                    setTimeLeft();
                    return;
                }
                if (bytesPerSecond > Math.Pow(2, 20))
                {
                    bytesPerSecond = bytesPerSecond / Math.Pow(2, 20);
                    m_pLblTransferRate.Text = String.Format("{0:f3} MB/s", bytesPerSecond);
                    setTimeLeft();
                    return;
                }
                if (bytesPerSecond > Math.Pow(2, 10))
                {
                    bytesPerSecond = bytesPerSecond / Math.Pow(2, 10);
                    m_pLblTransferRate.Text = String.Format("{0:f3} KB/s", bytesPerSecond);
                    setTimeLeft();
                    return;
                }
                if (bytesPerSecond < 1024)
                {
                    m_pLblTransferRate.Text = String.Format("{0:f3} B/s", bytesPerSecond);
                    setTimeLeft();
                    return;
                }
            }
        }

        public void setTimeLeft()
        {
            timeLeft = (iTotalSize - iCurrentDownloaded) / iTransferRate;
            int h=0,m=0,s=0;
            if(timeLeft > 3600)
            { 
                h = (int)(timeLeft / 3600);
                m = (int)timeLeft % 3600;

                if(m>60)
                {
                    m = m / 60;
                    s = m % 60;
                }
                strTimeLeft = String.Format("{0:d} hours:", h);
                strTimeLeft += String.Format(" {0:d} min:", m);
                strTimeLeft += String.Format(" {0:d} sec", s);

                m_pLblTimeLeft.Text = strTimeLeft;
                return;
            }

            if(timeLeft > 60)
            {
                m = (int)(timeLeft / 60);
                s = (int)timeLeft % 60;
                strTimeLeft = String.Format("{0:d} hours:", h);
                strTimeLeft += String.Format(" {0:d} min:", m);
                strTimeLeft += String.Format(" {0:d} sec ", s);

                m_pLblTimeLeft.Text = strTimeLeft;
                return;
            }

            s = (int)timeLeft;
            strTimeLeft = String.Format("{0:d} sec", s);
            m_pLblTimeLeft.Text = strTimeLeft;
            
        }

        public void addToList(ListViewItem lstItem)
        {
            m_pLsvThreadProcess.Items.Add(lstItem);
        }

        public void setThreadProcess(int index ,ListViewItem lstItem)
        {
            lock (syncLock)
            {
                if (index == -1) 
                    return;

                try
                {
                    m_pLsvThreadProcess.Items[index] = new ListViewItem("");
                    m_pLsvThreadProcess.Items[index] = lstItem;
                }
                catch (System.Exception ex)
                {
                	
                }
                
            }
        }
    }
}
